#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtCore/QDateTime>
#include <QtWidgets/QMessageBox>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QLCDNumber>


#define subStrFound  "esp8266/found"
#define subStrFlame  "esp8266/flame"
#define subStrTemp   "esp8266/temp"
#define subStrHumi   "esp8266/humi"
#define subStrToxi   "esp8266/toxi"
#define subStrState  "esp8266/state"
#define subStrCtrl  "esp8266/control"
#define subStrConnect "esp8266/connect"

int num = 0;
int flagnight = 0;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    speak = new QTextToSpeech;

    toxictrl();
    foundctrl();
    flamectrl();
    tempctrl();
    humictrl();
    statectrl();
    ui->connectEdit->setText("没有连接");
    connectctrl();
}


MainWindow::~MainWindow()
{
    delete ui;
    qDebug("close");
}

void MainWindow::humictrl()
{
    //创建一个客户端
    humi_client = new QMqttClient(this);
    humi_client->setHostname(QString("test.ranye-iot.net"));
    humi_client->setPort(1883);
    humi_client->connectToHost();  //连接服务器
    connect(humi_client, &QMqttClient::stateChanged, this,&MainWindow::subhumi);
    connect(humi_client, &QMqttClient::messageReceived, this,\
        [=](const QByteArray &message, const QMqttTopicName &topic) {
        QString content,content1;
        content += message;
        content1 = topic.name();
        ui->humiNumber->display(content);
    });
}
void MainWindow::subhumi()
{
    if(humi_client->state()==2){
        //连接客户端和订阅
        auto subscription = humi_client->subscribe(QString(subStrHumi), 0);
        if (!subscription) {
           qDebug() << "subhumi erro";
        }else
            qDebug() << "success subscription Humi";}
}

void MainWindow::tempctrl()
{
    //创建一个客户端
    temp_client = new QMqttClient(this);
    temp_client->setHostname(QString("test.ranye-iot.net"));
    temp_client->setPort(1883);
    temp_client->connectToHost();  //连接服务器
    connect(temp_client, &QMqttClient::stateChanged, this,&MainWindow::subtemp);
    connect(temp_client, &QMqttClient::messageReceived, this,\
        [=](const QByteArray &message, const QMqttTopicName &topic) {
        QString content,content1;
        content += message;
        content1 = topic.name();
        ui->tempNumber->display(content);
 //       qDebug()<<"temp recevice";
    });
}
void MainWindow::subtemp()
{
    if(temp_client->state()==2){
        //连接客户端和订阅
        auto subscription = temp_client->subscribe(QString(subStrTemp), 0);
        if (!subscription) {
           qDebug() << "subtemp erro";
        }else
            qDebug() << "success subscription temp";}
}
void MainWindow::flamectrl()
{
    //创建一个客户端
    flame_client = new QMqttClient(this);
    flame_client->setHostname(QString("test.ranye-iot.net"));
    flame_client->setPort(1883);
    flame_client->connectToHost();  //连接服务器
    connect(flame_client, &QMqttClient::stateChanged, this,&MainWindow::subflame);
    connect(flame_client, &QMqttClient::messageReceived, this,\
        [=](const QByteArray &message, const QMqttTopicName &topic) {
        QString content,content1;
        content += message;
        content1 = topic.name();
        if(QString::compare(content,"HIGH")==0)
        {
           ui->flameEdit->setText("警报发现火焰");
           speak->say("警报发现火焰");
        }
          else if(QString::compare(content,"LOW")==0)
           ui->flameEdit->setText("无");
   //     qDebug()<<"flame recevice";
    });
}

void MainWindow::subflame()
{
    if(flame_client->state()==2){
        //连接客户端和订阅
        auto subscription = flame_client->subscribe(QString(subStrFlame), 0);
        if (!subscription) {
           qDebug() << "subflame erro";
        }else
            qDebug() << "success subscription flame";}
}
//
void MainWindow::foundctrl()
{
    //创建一个客户端
    found_client = new QMqttClient(this);
    found_client->setHostname(QString("test.ranye-iot.net"));
    found_client->setPort(1883);
    found_client->connectToHost();  //连接服务器
    connect(found_client, &QMqttClient::stateChanged, this,&MainWindow::subfound);
    connect(found_client, &QMqttClient::messageReceived, this,\
        [=](const QByteArray &message, const QMqttTopicName &topic) {
        QString content,content1;
        content += message;
        content1 = topic.name();
        if(QString::compare(content,"HIGH")==0)
        {
           ui->foundEdit->setText("发现有人");
           if(flagnight)
           speak->say("警报发现有人");
           num++;
           ui->foundNumber->display(num);
        }
        else if(QString::compare(content,"LOW")==0)
           ui->foundEdit->setText("无");
 //       qDebug()<<content<<content1;
    });
}
/*   订阅主题 found 主题
*/
void MainWindow::subfound()
{
    if(found_client->state()==2){
        //连接客户端和订阅
        auto subscription = found_client->subscribe(QString(subStrFound), 0);
        if (!subscription) {
           qDebug() << "subfound erro";
        }else
            qDebug() << "success subscription Found";}
}

void MainWindow::toxictrl()
{
    //创建一个客户端
    toxi_client = new QMqttClient(this);
    toxi_client->setHostname(QString("test.ranye-iot.net"));
    toxi_client->setPort(1883);
    toxi_client->connectToHost();  //连接服务器
    connect(toxi_client, &QMqttClient::stateChanged, this,&MainWindow::subtoxi);
    connect(toxi_client, &QMqttClient::messageReceived, this,\
        [=](const QByteArray &message, const QMqttTopicName &topic) {
        QString content,content1;
        content += message;
        content1 = topic.name();
        if(QString::compare(content,"HIGH")==0)
        {
           ui->toxiEdit->setText("发现有毒气体");
           speak->say("警报发现有毒气体");
        }
         else if(QString::compare(content,"LOW")==0)
           ui->toxiEdit->setText("无");
 //       qDebug()<<content<<content1;
    });
}

void MainWindow::subtoxi()
{
    if(toxi_client->state()==2){
        //连接客户端和订阅
        auto subscription = toxi_client->subscribe(QString(subStrToxi), 0);
        if (!subscription) {
           qDebug() << "subtoxi error";
        }else
            qDebug() << "success subscription toxi";}
}
void MainWindow::statectrl()
{
    //创建一个客户端
    state_client = new QMqttClient(this);
    state_client->setHostname(QString("test.ranye-iot.net"));
    state_client->setPort(1883);
    state_client->connectToHost();  //连接服务器
    connect(state_client, &QMqttClient::stateChanged, this,&MainWindow::substate);
    connect(state_client, &QMqttClient::messageReceived, this,\
        [=](const QByteArray &message, const QMqttTopicName &topic) {
        QString content,content1;
        content += message;
        content1 = topic.name();
        if(QString::compare(content,"UP")==0)
        {
           ui->stateEdit->setText("移动前进");
        }
         else if(QString::compare(content,"BLACK")==0)
           ui->stateEdit->setText("后退");
        else if(QString::compare(content,"STOP")==0)
          ui->stateEdit->setText("停止移动");
        else if(QString::compare(content,"LEFT")==0)
          ui->stateEdit->setText("左转");
        else if(QString::compare(content,"RIGHT")==0)
          ui->stateEdit->setText("右转");
        else if(QString::compare(content,"AUTO")==0)
          ui->stateEdit->setText("自动循迹模式");
        else if(QString::compare(content,"BEEP")==0){
          ui->stateEdit->setText("鸣笛");
          speak->say("蜂鸣器已经打开");}
        else if(QString::compare(content,"NIGHT")==0){
           ui->stateEdit->setText("夜间模式");
           if(flagnight)
               flagnight = 0;
           else
               flagnight++;
        }
    });
}
/*   订阅主题 found 主题
*/
void MainWindow::substate()
{
    if(state_client->state()==2){
        //连接客户端和订阅
        auto subscription = state_client->subscribe(QString(subStrState), 0);
        if (!subscription) {
           qDebug() << "substate error";
        }else
            qDebug() << "success subscription state";}
}

void MainWindow::connectctrl()
{
    //创建一个客户端
    connect_client = new QMqttClient(this);
    connect_client->setHostname(QString("test.ranye-iot.net"));
    connect_client->setPort(1883);
    connect_client->connectToHost();  //连接服务器
    connect(connect_client, &QMqttClient::stateChanged, this,&MainWindow::subconnect);
    connect(connect_client, &QMqttClient::messageReceived, this,\
        [=](const QByteArray &message, const QMqttTopicName &topic) {
        QString content,content1;
        content += message;
        content1 = topic.name();
        if(QString::compare(content,"OK")==0)
        {
           ui->connectEdit->setText("连接成功");
        }
 //       qDebug()<<content<<content1;
    });
}

void MainWindow::subconnect()
{
    if(connect_client->state()==2){
        //连接客户端和订阅
        auto subscription = connect_client->subscribe(QString(subStrConnect), 0);
        if (!subscription) {
           qDebug() << "subconnect error";
        }else
            qDebug() << "success subscription connect";}
}

//退出按钮
void MainWindow::on_quit_clicked()
{
    found_client->disconnect();//断开服务器
    flame_client->disconnect();
    temp_client->disconnect();
    humi_client->disconnect();
    toxi_client->disconnect();
    state_client->disconnect();
    connect_client->disconnect();
    QApplication::quit();
}

